package com.naiterui.ehp.bs.doctor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.naiterui.ehp.bp.bo.patient.PatientInquirerBO;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.encypt.DESUtil;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bs.doctor.fegin.PatientFeignClient;
import com.naiterui.ehp.bs.doctor.service.IConsultSessionService;
import com.naiterui.ehp.bs.doctor.service.IPatientInquirerService;
import com.naiterui.ehp.bs.doctor.vo.patient.InquirerRelationVO;
import com.naiterui.ehp.bs.doctor.vo.patient.InquirerVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 就诊人信息
 * @Author gongbaoqiang
 * @Date 2020/7/10 3:47 PM
 */
@Slf4j
@Service
public class AdPatientInquirerServiceImpl implements IPatientInquirerService {

    @Autowired
    private IConsultSessionService consultSessionService;

    @Autowired
    private PatientFeignClient patientFeignClient;

    @Override
    public List<InquirerVO> list(Long patientId, Long doctorId) throws BusinessException {
        return patientFeignClient.patientInquirerListByPatientId(patientId).stream().map(s -> {
            InquirerVO vo = new InquirerVO();
            BeanUtils.copyProperties(s, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    private String getOfflineMedicalRecordUrl(String idCard, String name) {
        try {
            String url = ConfigUtil.getString("his.recordPageUrl");
            String key = DESUtil.desEncrypt(ConfigUtil.getString("his.hisSecret"), idCard + "|" + name);
            return String.format(url, URLEncoder.encode(key, "UTF-8"));
        }catch (Exception e){
            log.error("生成线下HIS地址失败", e);
        }
        return "";
    }

    @Override
    public InquirerVO detail(String sessionId, Long patientId, Long doctorId) throws BusinessException {
        if (StrUtil.isBlank(sessionId) && patientId != null) {
            PatientInquirerBO patientInquirerBO = patientFeignClient.getDefultInquirerInfoByPatientId(patientId, doctorId);
            InquirerVO vo = new InquirerVO();
            BeanUtils.copyProperties(patientInquirerBO, vo);
            vo.setOfflineMedicalRecordUrl(getOfflineMedicalRecordUrl(vo.getIdCard(), vo.getName()));
            return vo;
        }
        Long inquirerId = consultSessionService.findInquirerBySessionId(sessionId);
        List<InquirerVO> list = patientFeignClient.patientInquirerList(Lists.newArrayList(inquirerId)).stream().map(s -> {
            InquirerVO vo = new InquirerVO();
            BeanUtils.copyProperties(s, vo);
            vo.setOfflineMedicalRecordUrl(getOfflineMedicalRecordUrl(vo.getIdCard(), vo.getName()));
            return vo;
        }).collect(Collectors.toList());
        return list.get(0);
    }

    @Override
    public List<InquirerRelationVO> relationList(Long patientId, Long doctorId) throws BusinessException {
        List<InquirerRelationVO> result = Lists.newArrayList();
        result.addAll(patientFeignClient.patientInquirerRelationList(patientId));
        result.add(InquirerRelationVO.builder()
                .relationId(-1)
                .relationName("全部")
                .build());
        return result;
    }


}
